/**
 * 工单相关 API
 */
import http from '../utils/request'

export interface WorkOrder {
  id: number
  work_order_number: string
  title: string
  description?: string
  device_id: number
  device_code?: string
  device_name?: string
  reporter_id: number
  reporter_name?: string
  assignee_id?: number
  assignee_name?: string
  status: string
  priority: string
  work_order_type: string
  location?: string
  estimated_duration?: number
  actual_duration?: number
  completion_notes?: string
  created_at: string
  updated_at: string
  assigned_at?: string
  started_at?: string
  completed_at?: string
  due_date?: string
}

export interface WorkOrderListQuery {
  page?: number
  page_size?: number
  search?: string
  status?: string
  priority?: string
  work_order_type?: string
  assignee_id?: number
  reporter_id?: number
  device_id?: number
  location?: string
  start_date?: string
  end_date?: string
  overdue_only?: boolean
  sort_by?: string
  sort_order?: string
}

export interface WorkOrderCreate {
  title: string
  description?: string
  device_id: number
  priority: string
  work_order_type: string
  location?: string
  estimated_duration?: number
  required_skills?: string[]
  materials_needed?: string[]
}

export interface WorkOrderUpdate {
  title?: string
  description?: string
  priority?: string
  estimated_duration?: number
  required_skills?: string[]
  materials_needed?: string[]
}

export interface WorkOrderAssign {
  assignee_id: number
  notes?: string
  due_date?: string
}

export interface WorkOrderStatusUpdate {
  status: string
  notes?: string
  completion_notes?: string
  actual_duration?: number
  materials_used?: string[]
}

// 获取工单列表
export const getWorkOrderList = (params?: WorkOrderListQuery) => {
  return http.get('/work-orders', { params })
}

// 获取工单详情
export const getWorkOrderDetail = (id: number) => {
  return http.get(`/work-orders/${id}`)
}

// 创建工单
export const createWorkOrder = (data: WorkOrderCreate) => {
  return http.post('/work-orders', data)
}

// 更新工单信息
export const updateWorkOrder = (id: number, data: WorkOrderUpdate) => {
  return http.put(`/work-orders/${id}`, data)
}

// 分配工单
export const assignWorkOrder = (id: number, data: WorkOrderAssign) => {
  return http.post(`/work-orders/${id}/assign`, data)
}

// 更新工单状态
export const updateWorkOrderStatus = (id: number, data: WorkOrderStatusUpdate) => {
  return http.patch(`/work-orders/${id}/status`, data)
}

// 添加工单评论
export const addWorkOrderComment = (id: number, data: {
  content: string
  is_internal?: boolean
}) => {
  return http.post(`/work-orders/${id}/comments`, data)
}

// 删除工单
export const deleteWorkOrder = (id: number) => {
  return http.delete(`/work-orders/${id}`)
}

// 批量操作工单
export const batchOperateWorkOrders = (data: {
  work_order_ids: number[]
  operation: string
  params?: Record<string, any>
}) => {
  return http.post('/work-orders/batch-operation', data)
}

// 获取工单统计
export const getWorkOrderStatistics = () => {
  return http.get('/work-orders/statistics/dashboard')
}